package com.cy.game

import androidx.activity.viewModels
import androidx.fragment.app.Fragment
import com.helloandroid.AppUtil
import com.helloandroid.R
import com.helloandroid.base.BaseActivity
import com.helloandroid.tools.MyLog
import com.helloandroid.tools.NetWorkUtil

class CyHomeActivity : BaseActivity() {

    companion object {
        const val TenMinutes = 10 * 60 * 1000L
    }

    // 临时 cache
    var mInfo: CyInfo? = null

    // 用于倒计时的时间
    var countDownTimeMs = 0L

    val model: CyViewModel by viewModels()
    var isReady = false

    var curFragment: Fragment? = null

    override fun getContentViewResId(): Int = R.layout.cy_home

    override fun initView() {
        curFragment = CyHomeFragment()
        supportFragmentManager.beginTransaction().replace(R.id.layout, curFragment!!).commit()
    }

    override fun initData() {
        model.getInfo { success, info ->
            isReady = success
            if (success) {
                MyLog.info("info:$info")
                mInfo = info
                if (curFragment is CyHomeFragment) {
                    val home = curFragment as CyHomeFragment
                    if (home.inited){
                        home.initUI()
                    }
                }
            }
        }
        model.startPerSecondTick(this::perSecondTick)
    }

    private fun perSecondTick() {
        if (!isReady) {
            return
        }

        val cur = AppUtil.sysTime
        val info = mInfo!!
        if (info.power >= 20) {
            countDownTimeMs = 0L
        } else {
            countDownTimeMs += 1000
            if (countDownTimeMs >= TenMinutes) {
                // 恢复一个体力
                countDownTimeMs = 0L
                AppUtil.appViewModel().cyRepower(cur, 1, false)
                info.increasePower(1)
                info.lastRepowerMs = cur
            }
        }

        if (curFragment != null) {
            if (curFragment is CyHomeFragment) {
                (curFragment as CyHomeFragment).tick()
            }
        }
    }

    fun navToCyHome(){
        if (curFragment !is CyHomeFragment) {
            curFragment = CyHomeFragment()
            supportFragmentManager.beginTransaction().replace(R.id.layout, curFragment!!).commit()
        }
    }

    fun navToCyGame(){
        curFragment = CyGameFragment()
        supportFragmentManager.beginTransaction().replace(R.id.layout, curFragment!!).commit()
    }

    override fun onBackPressed() {
        if (curFragment != null){
            if (curFragment is CyHomeFragment){
                super.onBackPressed()
            }
        }
    }

    fun realStartGame(info:CyInfo){
        // 判断体力是否够
        if (info.power < 1) {
            // 弹出补充体力窗口
            AddPowerDialog(this).show()
        } else {
            if (!NetWorkUtil.networkConnected()) {
                AppUtil.toast("请打开网络!")
                return
            }
            AppUtil.appViewModel().cyConsumePower()
            info.decreasePower(1)
            this.navToCyGame()
        }
    }

    override fun isDarkMode() = false
}